package progressBugBusiness;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import easyDAO.SqlDispatch;
import impl.TestServerBusinessImpl;
import models.GetRequestInputStream;
import models.MyConfig;
import models.ReturnJson;
import models.SqlReturnModel;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * Created by yangxianming on 2021/2/23.
 * x
 */
public class ShowProgressBug implements TestServerBusinessImpl {

    @Override
    public String doIt(HttpServletRequest request, MyConfig myConfig){
        JSONObject requestInfo = GetRequestInputStream.getJson(request);
        ReturnJson returnJson = new ReturnJson();
        SqlDispatch sqlDispatch = new SqlDispatch(myConfig);
        JSONArray result = new JSONArray();
        String action = requestInfo.getString("action");

        if (Objects.equals("showBug", action)){
            JSONObject data = requestInfo.getJSONObject("data");
            String progressname = data.getString("progressname");
            String key = data.getString("key");
            String owner = data.getString("owner");
            String project = data.getString("project");
            String state = data.getString("state");
            String createtime = data.getString("createtime");
            //列出相关bug
            progressname = !Objects.equals(progressname, "") ? "`progressname` = '"+progressname+"' AND " : "";
            key = !Objects.equals(key, "") ? "`title` LIKE '%"+key+"%' OR `describe` LIKE '%"+key+"%' AND " : "";
            owner = !Objects.equals(owner, "") ? "`owner` = '"+owner+"' AND " : "";
            project = !Objects.equals(project, "") ? "`progressname` LIKE '"+project+"%' AND " : "";
            state = !Objects.equals(state, "0")  ? "`state` = '"+state+"' AND " : "";
            createtime = !Objects.equals(createtime, "") ? "`createtime` LIKE '"+createtime+"%' AND " : "";
            String sql = "SELECT * FROM progressbug WHERE " + progressname + key + owner + project + state + createtime;
            sql = sql.trim();
            if (sql.endsWith("WHERE") || sql.endsWith("AND")){
                sql = sql.substring(0, sql.lastIndexOf(" "));
            }
            sql = sql + "ORDER BY `createtime` DESC";
            System.out.println(sql);
            SqlReturnModel srm = sqlDispatch.selectWord(sql);

            returnJson.setResult(srm.getSelectReturnData());
            returnJson.setApiStatus((srm.getIsSuccess()) ? 1 : 0);
            returnJson.setErrorMsg(srm.getFailingReason());
        }




        return returnJson.toString();
    }

}
